#include "cw32l010_flash.h"
#include "cw32l010_sysctrl.h"
#include "bsp_flash.h"

void bsp_flash_init(void)
{
	__SYSCTRL_FLASH_CLK_ENABLE();
	FLASH_SetLatency(FLASH_Latency_1);
}

int bsp_flash_page_erase(unsigned char page)
{
	unsigned char Flag = 0;
	FLASH_UnlockPage(page);
	Flag = FLASH_ErasePage(page);
	FLASH_LockAllPages();
	if (Flag)
		return 1;
	return 0;
}

int bsp_flash_page_write(unsigned char page, unsigned char *data, unsigned short len)
{
	unsigned char Flag;
	FLASH_UnlockPage(page);			  // 解锁最后一个页面
	Flag = FLASH_WriteBytes(page*512, data, len); // 127页按字节写入
	FLASH_LockAllPages();
	if (Flag)
		return 1;
	return 0;
}
